#!/bin/bash
#
# $1: The full path to the installation package.
# $2: The full path to the installation destination.
# $3: The mountpoint of the destination volume.
# $4: The root directory for the current System folder.
#
# (c) Copyright 2006/2007/2008/2009 Andreas Fink <andreas@fink.org>

LOG="/var/log/sctp.log"

echo "*** Postinstall ***" >> $LOG
echo \$1 = $1 >> $LOG
echo \$2 = $2 >> $LOG
echo \$3 = $3 >> $LOG
echo \$4 = $4 >> $LOG
#

echo touch /System/Library/Extensions >> $LOG 2>> $LOG
touch /System/Library/Extensions >> $LOG 2>> $LOG

echo chown -R root:wheel /System/Library/Extensions/SCTP.kext/  >> $LOG 2>> $LOG
chown -R root:wheel /System/Library/Extensions/SCTP.kext/ >> $LOG 2>> $LOG

echo chmod 755 /System/Library/Extensions/SCTP.kext/  >> $LOG 2>> $LOG
chmod 755 /System/Library/Extensions/SCTP.kext/ >> $LOG 2>> $LOG

echo chmod 755 /System/Library/Extensions/SCTP.kext/Contents/  >> $LOG 2>> $LOG
chmod 755 /System/Library/Extensions/SCTP.kext/Contents/ >> $LOG 2>> $LOG

echo chmod 644 /System/Library/Extensions/SCTP.kext/Contents/Info.plist  >> $LOG 2>> $LOG
chmod 644 /System/Library/Extensions/SCTP.kext/Contents/Info.plist  >> $LOG 2>> $LOG

echo chmod 644 /System/Library/Extensions/SCTP.kext/Contents/Info.plist.template  >> $LOG 2>> $LOG
chmod 644 /System/Library/Extensions/SCTP.kext/Contents/Info.plist.template  >> $LOG 2>> $LOG

echo chmod 755 /System/Library/Extensions/SCTP.kext/Contents/MacOS  >> $LOG 2>> $LOG
chmod 755 /System/Library/Extensions/SCTP.kext/Contents/MacOS  >> $LOG 2>> $LOG

echo chmod 644 /System/Library/Extensions/SCTP.kext/Contents/MacOS/SCTP  >> $LOG 2>> $LOG
chmod 644 /System/Library/Extensions/SCTP.kext/Contents/MacOS/SCTP  >> $LOG 2>> $LOG

echo chmod 755 /System/Library/Extensions/SCTP.kext/Contents/Resources  >> $LOG 2>> $LOG
chmod 755 /System/Library/Extensions/SCTP.kext/Contents/Resources  >> $LOG 2>> $LOG

echo chmod 755 /System/Library/Extensions/SCTP.kext/Contents/Resources/English.lproj  >> $LOG 2>> $LOG
chmod 755 /System/Library/Extensions/SCTP.kext/Contents/Resources/English.lproj  >> $LOG 2>> $LOG

echo chmod 644 /System/Library/Extensions/SCTP.kext/Contents/Resources/English.lproj/InfoPlist.strings  >> $LOG 2>> $LOG
chmod 644 /System/Library/Extensions/SCTP.kext/Contents/Resources/English.lproj/InfoPlist.strings  >> $LOG 2>> $LOG

echo chown -R root:wheel /Library/StartupItems/SCTP  >> $LOG 2>> $LOG
chown -R root:wheel /Library/StartupItems/SCTP >> $LOG 2>> $LOG

echo chmod 755 /Library/StartupItems/SCTP/SCTP  >> $LOG 2>> $LOG
chmod 755 /Library/StartupItems/SCTP/SCTP  >> $LOG 2>> $LOG

echo chmod 644 /Library/StartupItems/SCTP/StartupParameters.plist >> $LOG 2>> $LOG
chmod 644 /Library/StartupItems/SCTP/StartupParameters.plist  >> $LOG 2>> $LOG

#
echo "/Library/StartupItems/SCTP/SCTP prepare" >> $LOG 2>> $LOG
if /Library/StartupItems/SCTP/SCTP prepare >> $LOG 2>> $LOG
then 
   echo "*SUCCESS*" >> $LOG 2>> $LOG
else
   echo "*FAILED*" >> $LOG 2>> $LOG
fi

echo "/Library/StartupItems/SCTP/SCTP start" >> $LOG 2>> $LOG
if /Library/StartupItems/SCTP/SCTP start >> $LOG 2>> $LOG
then 
   echo "*SUCCESS*" >> $LOG 2>> $LOG
else
   echo "*FAILED*" >> $LOG 2>> $LOG
fi

#
# we must check if there are old versions of the headerfiles inside the SDK dirs.
# we link them to the current one instead
# also {SDK}/usr might be linked to /usr so we want to make sure we dont delete the original by mistake
#

for SDKVER in "MacOSX10.6.sdk" "MacOSX10.5.sdk" "MacOSX10.4u.sdk"
do
    SDKDIR="/Developer/SDKs/${SDKVER}"
    echo "SDKVER=${SDKVER}" >> $LOG 2>> $LOG
    echo "SDKDIR=${SDKDIR}" >> $LOG 2>> $LOG
    if [ -d "${SDKDIR}" ]
    then
        if [ -d "${SDKDIR}/usr" ]
        then
            if [ -d "${SDKDIR}/usr/include" ]
            then
                if [ -d "${SDKDIR}/usr/include/netinet" ]
                then
                    if [ -f "${SDKDIR}/usr/include/netinet/sctp.h" -o -L "${SDKDIR}/usr/include/netinet/sctp.h" ]
                    then
                        echo "rm -f \"${SDKDIR}/usr/include/netinet/sctp.h\"" >> $LOG 2>> $LOG
                        rm -f "${SDKDIR}/usr/include/netinet/sctp.h" >> $LOG 2>> $LOG
                    fi
                    echo "ln -s /usr/include/netinet/sctp.h \"${SDKDIR}/usr/include/netinet/sctp.h\"" >> $LOG 2>> $LOG
                    ln -s /usr/include/netinet/sctp.h "${SDKDIR}/usr/include/netinet/sctp.h" >> $LOG 2>> $LOG
                    if [ -f "${SDKDIR}/usr/include/netinet/sctp_uio.h" -o -L "${SDKDIR}/usr/include/netinet/sctp_uio.h" ]
                    then
                        echo "rm -f \"${SDKDIR}/usr/include/netinet/sctp_uio.h\"" >> $LOG 2>> $LOG
                        rm -f "${SDKDIR}/usr/include/netinet/sctp_uio.h" >> $LOG 2>> $LOG
                    fi
                    echo "ln -s /usr/include/netinet/sctp_uio.h \"${SDKDIR}/usr/include/netinet/sctp_uio.h\"" >> $LOG 2>> $LOG
                    ln -s /usr/include/netinet/sctp_uio.h "${SDKDIR}/usr/include/netinet/sctp_uio.h" >> $LOG 2>> $LOG
                fi
            fi
            if [ -d "${SDKDIR}/usr/lib" ]
            then
                if [ -f "${SDKDIR}/usr/lib/libsctp.dylib" -o -L "${SDKDIR}/usr/lib/libsctp.dylib" ]
                then
                    echo "rm -f \"${SDKDIR}/usr/lib/libsctp.dylib\"" >> $LOG 2>> $LOG
                    rm -f "${SDKDIR}/usr/lib/libsctp.dylib" >> $LOG 2>> $LOG
                fi
                echo "ln -s /usr/lib/libsctp.dylib \"${SDKDIR}/usr/lib/libsctp.dylib\"" >> $LOG 2>> $LOG
                ln -s /usr/lib/libsctp.dylib "${SDKDIR}/usr/lib/libsctp.dylib" >> $LOG 2>> $LOG
            fi
        fi
    else
        echo "SDKDIR='${SDKDIR}' not found" >> $LOG 2>> $LOG
    fi
done
echo "*** Postinstall completed *** ">> $LOG 2>> $LOG
exit 0
